package net.ojbk.dao;

import java.sql.*;

import net.ojbk.database.DatabaseConnection;
import net.ojbk.po.UserLogin;
/**
 * UserLogin Table
 * 
 * 增加 一条记录
 * 删除 一条记录
 * 查询 一条记录
 * 修改 一条记录
 * 
 * @author twobox
 *
 */
public class UserLoginDao {
	static final String tableName="userLogin";
	DatabaseConnection dbconn;
	Statement stmt;
	
	public UserLoginDao(){
		dbconn=new DatabaseConnection();
		stmt=dbconn.getStatement();
	}
	public void finalize()
	{
		dbconn.close();
	}
	
	public boolean addOneRecord(UserLogin ul)
	{
		boolean flag=false;
		try {
			String sql="INSERT INTO "
					+tableName
					+"(id, password) VALUES('"
					+ul.getId()
					+"', '"
					+ul.getPassword()
					+"');";
			stmt.executeUpdate(sql);
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return flag;
	}
	public boolean deleteOneRecord(UserLogin ul)
	{
		boolean flag=false;
		try {
			String sql="DELETE FROM "
					+tableName
					+" WHERE id='"
					+ul.getId()
					+"';";
			stmt.executeUpdate(sql);
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}
	public boolean updateOneRecord(UserLogin ul)
	{
		boolean flag=false;
		try {
		String sql="UPDATE "
				+tableName
				+" SET id='"
				+ul.getId()
				+"', password='"
				+ul.getPassword()
				+"' WHERE id='"
				+ul.getId()
				+"';";
		stmt.executeUpdate(sql);
		flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public boolean queryOneRecord(UserLogin ul)
	{
		boolean flag=false;
		ResultSet re;
		try {
		String sql="SELECT * FROM "
				+tableName
				+" WHERE id='"
				+ul.getId()
				+"' ;";
		re=stmt.executeQuery(sql);
		if(re.next())
		{
			ul.setId(re.getString("id"));
			ul.setPassword(re.getString("password"));
			flag=true;
		}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	public static void main(String[] args)
	{
		UserLoginDao uld=new UserLoginDao();
		UserLogin ul=new UserLogin();
		ul.setId("10010");
		ul.setPassword("100024");
		uld.deleteOneRecord(ul);
	}
}
